local RegisterLayer = class("RegisterLayer", require "widget/BaseUI")
 
function RegisterLayer.show()
    print("============RegisterLayer.show===============")
 
    local win = RegisterLayer.new()

    local data = getWndInitParamList()
    data.isRemoveResource = false
    data.imgTitle = nil
    data.bgImage = nil
    data.isShowCover = false,

    win:init(data)
    win:showWindow()

    return win

end

function RegisterLayer:ctor() 
    self.super:ctor();
end


function RegisterLayer:init(data)
    self:initWithParas(data)
    
    self.m_Json = ccs.GUIReader:getInstance():widgetFromJsonFile("ui/launcher/reguster.Json")
    self.m_Json:setPositionX(-WinSize.width/2)
    self.m_Json:setPositionY(-WinSize.height/2) 
    self:setContentSize(self.m_Json:getContentSize())
    local WinSize = cc.Director:getInstance():getWinSize()
    self.m_Json:setScaleX(WinSize.width/self.m_Json:getContentSize().width)
    self.m_Json:setScaleY(WinSize.height/self.m_Json:getContentSize().height)
    self:addChild(self.m_Json)
    
    self.m_TextFieldAccounts = ccui.Helper:seekWidgetByName(self.m_Json,"TextFieldAccount")
    self.m_TextFieldAccounts:setPlaceHolder(LocalizedString("register_account_tips"))
    self.m_TextFieldAccounts:setFontSize(14)
    self.m_TextFieldAccounts:setMaxLengthEnabled(true)
    self.m_TextFieldAccounts:setMaxLength(10)
    self.m_TextFieldAccounts:setText("")
    self.m_TextFieldAccounts:addEventListener(handler(self, self.textFieldEvent))
    
    
    self.m_TextFieldPassword = ccui.Helper:seekWidgetByName(self.m_Json,"TextFieldPassword")
    self.m_TextFieldPassword:setPlaceHolder(LocalizedString("register_password_tips"))
    self.m_TextFieldPassword:setFontName(DEFAULT_FONT)
    self.m_TextFieldPassword:setFontSize(14)
    self.m_TextFieldPassword:setPasswordEnabled(true)
    self.m_TextFieldPassword:setPasswordStyleText("*")
    self.m_TextFieldPassword:setMaxLengthEnabled(true)
    self.m_TextFieldPassword:setMaxLength(6)
    self.m_TextFieldPassword:setText("")
    self.m_TextFieldPassword:addEventListener(handler(self, self.textFieldEvent))
    
    
    self.m_TextFieldRePassword = ccui.Helper:seekWidgetByName(self.m_Json,"TextFieldRePassword")
    self.m_TextFieldRePassword:setPlaceHolder(LocalizedString("register_repassword_tips"))
    self.m_TextFieldRePassword:setFontName(DEFAULT_FONT)
    self.m_TextFieldRePassword:setFontSize(14)
    self.m_TextFieldRePassword:setPasswordEnabled(true)
    self.m_TextFieldRePassword:setPasswordStyleText("*")
    self.m_TextFieldRePassword:setMaxLengthEnabled(true)
    self.m_TextFieldRePassword:setMaxLength(6)
    self.m_TextFieldRePassword:setText("")
    self.m_TextFieldRePassword:addEventListener(handler(self, self.textFieldEvent))
   
    
    self.m_TextFieldInviter = ccui.Helper:seekWidgetByName(self.m_Json,"TextFieldInviter")
    self.m_TextFieldInviter:setPlaceHolder(LocalizedString("register_inviter_tips"))
    self.m_TextFieldInviter:setFontSize(14)
    self.m_TextFieldInviter:setMaxLengthEnabled(true)
    self.m_TextFieldInviter:setMaxLength(10)
    self.m_TextFieldInviter:setText("")
    self.m_TextFieldInviter:addEventListener(handler(self, self.textFieldEvent))
    
    self.m_TextFieldAccounts:setText("test001")
    self.m_TextFieldPassword:setText("123456")
    self.m_TextFieldRePassword:setText("123456")
    self.m_TextFieldInviter:setText("10086")

    
 
    self.m_BtnOK = self.m_Json:getChildByName("BtnOK")
    self.m_BtnOK:setTouchEnabled(true)
    self.m_BtnOK:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)

    self.m_BtnCancel = self.m_Json:getChildByName("BtnCancel")
    self.m_BtnCancel:setTouchEnabled(true)
    self.m_BtnCancel:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)  
  
end

function RegisterLayer:textFieldEvent(sender, eventType)
    if eventType == ccui.TextFiledEventType.attach_with_ime then

    elseif eventType == ccui.TextFiledEventType.detach_with_ime then

    elseif eventType == ccui.TextFiledEventType.insert_text then

    elseif eventType == ccui.TextFiledEventType.delete_backward then

    end
end

function RegisterLayer:sendRegister()
    -- 开始登录
    local packout = dv.LuaPacket:new(300)
    local Account = self.m_TextFieldAccounts:getStringValue()
    local Password = self.m_TextFieldPassword:getStringValue()
    local RePassword = self.m_TextFieldRePassword:getStringValue()
    local Inviter = self.m_TextFieldInviter:getStringValue()
    
 
    local bodydata = Struct:getInstance():getBodyData('CMD_CS_HallUserRegister') 
    Struct:getInstance():setValue(bodydata, "iVersion", 100000)
    Struct:getInstance():setValue(bodydata, "iAppId", 102)
    Struct:getInstance():setValue(bodydata, "iPlatformNo", 1)
    Struct:getInstance():setValue(bodydata, "iEnterMode",0)
    Struct:getInstance():setValue(bodydata, "szMachineCode", "5644613231")
    Struct:getInstance():setValue(bodydata, "szName", Account)
    Struct:getInstance():setValue(bodydata, "szPwd", MD5String(Password))
    Struct:getInstance():setValue(bodydata, "szNickName", Account)
    Struct:getInstance():setValue(bodydata, "szIp", "127.0.0.1")
    Struct:getInstance():setValue(bodydata, "iSex", 1)
    Struct:getInstance():setValue(bodydata, "iTrenchId", 0)
    Struct:getInstance():setValue(bodydata, "szAgentCode", "10086")
    Struct:getInstance():writeBody(packout, bodydata)

    print("====MAIN_HALL_MSG_LOGIN========"..Cmd.MAIN_HALL_MSG_LOGIN.."===SUB_CS_USER_REGISTER_HALL==="..Cmd.SUB_CS_USER_REGISTER_HALL)

    SendData(Network.SERVER_LOGIN,Cmd.MAIN_HALL_MSG_LOGIN,Cmd.SUB_CS_USER_REGISTER_HALL,packout)
end

function RegisterLayer:onMenutClick(sender,eventType)
    if eventType == ccui.TouchEventType.ended then
        if sender == self.m_BtnOK then     
            print "=========RegisterLayer:onMenutClick m_BtnOK======================="
            self:sendRegister()
        elseif sender == self.m_BtnCancel then     
            print "=========RegisterLayer:onMenutClick m_BtnCancel======================="
            self:closeWindow()
        end
    end
end
 
return RegisterLayer